package com.hdphone.zljutils.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.hdphone.zljutils.inter.IGsonUtil;

import java.lang.reflect.Type;

/**
 * @author sq
 * @date 2020/10/20
 * @describe
 */
public class GsonUtilImpl implements IGsonUtil {

    private Gson gson = new GsonBuilder().create();

    @Override
    public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
        return gson.fromJson(json, classOfT);
    }

    @Override
    public <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
        return (T) gson.fromJson(json, typeOfT);
    }

    @Override
    public String toJson(Object value) {
        if (value == null) {
            return null;
        }
        return gson.toJson(value);
    }
}
